//
//  OrgAuthDetailViewController.m
//  Kjs-iOS
//
//  Created by 江伟 on 16/6/30.
//  Copyright © 2016年 jmhz. All rights reserved.
//

#import "OrgAuthDetailViewController.h"
#import "UIImage+MapUIImage.h"
#import "UploadIdentityViewController.h"
#import "UploadLicenseViewController.h"
#import "UploadPermissionViewController.h"
#import "AppDelegate.h"
#import "KJSRequest.h"
#import "CommonUtil.h"

@interface OrgAuthDetailViewController ()
@property (nonatomic,strong) NSData *idData;
@property (nonatomic,strong) NSString *idImageUrl;
@property (nonatomic,strong) NSData *licenseData;
@property (nonatomic,strong) NSString *licenseImageUrl;
@property (nonatomic,strong) NSData *permissionData;
@property (nonatomic,strong) NSString *permissionImageUrl;

@property (nonatomic,weak) IBOutlet UIButton *submintBtn;

@property (nonatomic,weak) IBOutlet UIImageView *identityArrowImgV;
@property (nonatomic,weak) IBOutlet UIImageView *licenseArrowImgV;
@property (nonatomic,weak) IBOutlet UIImageView *permissionArrowImgV;

@property (nonatomic,weak) IBOutlet UIView *identityView;
@property (nonatomic,weak) IBOutlet UIView *licenseView;
@property (nonatomic,weak) IBOutlet UIView *permissionView;

@property (nonatomic,weak) IBOutlet UIImageView *idImageV;
@property (nonatomic,weak) IBOutlet UIImageView *licenseImageV;
@property (nonatomic,weak) IBOutlet UIImageView *permissionImageV;

@property (nonatomic,weak) IBOutlet UITextField *oCompanyNameTF;
@property (nonatomic,weak) IBOutlet UITextField *oUanTF;
@property (nonatomic,weak) IBOutlet UITextField *oRepresentNameTF;
@property (nonatomic,weak) IBOutlet UITextField *oRepresentPhoneTF;
@property (nonatomic,weak) IBOutlet UITextField *oRepresentIdNumTF;

@end

@implementation OrgAuthDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.navigationItem setTitle:@"实名认证"];
    self.submintBtn.layer.cornerRadius = 10;
    self.submintBtn.layer.masksToBounds = YES;
    self.identityArrowImgV.image = [UIImage drawArrowWithDirection:DirectionTypeRight];
    self.licenseArrowImgV.image = [UIImage drawArrowWithDirection:DirectionTypeRight];
    self.permissionArrowImgV.image = [UIImage drawArrowWithDirection:DirectionTypeRight];
    self.idImageUrl = @"";
    self.licenseImageUrl = @"";
    self.permissionImageUrl = @"";
    
    self.identityView.tag = 1;
    self.identityView.userInteractionEnabled = YES;
    self.licenseView.tag = 2;
    self.licenseView.userInteractionEnabled = YES;
    self.permissionView.tag = 3;
    self.permissionView.userInteractionEnabled = YES;
    UITapGestureRecognizer *uploadIdentity = [[UITapGestureRecognizer alloc]  initWithTarget:self action:@selector(goUploadFile:)];
    UITapGestureRecognizer *uploadLicense = [[UITapGestureRecognizer alloc]  initWithTarget:self action:@selector(goUploadFile:)];
    UITapGestureRecognizer *uploadPermission = [[UITapGestureRecognizer alloc]  initWithTarget:self action:@selector(goUploadFile:)];
    [self.identityView addGestureRecognizer:uploadIdentity];
    [self.licenseView addGestureRecognizer:uploadLicense];
    [self.permissionView addGestureRecognizer:uploadPermission];
    
    [self.submintBtn addTarget:self action:@selector(submitAuth) forControlEvents:UIControlEventTouchUpInside];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    if (self.idData) {
        [self.idImageV setHidden:NO];
    }else{
        [self.idImageV setHidden:YES];
    }
    if (self.licenseData) {
        [self.licenseImageV setHidden:NO];
    }else{
        [self.licenseImageV setHidden:YES];
    }
    if (self.permissionData) {
        [self.permissionImageV setHidden:NO];
    }else{
        [self.permissionImageV setHidden:YES];
    }
}
#pragma  mark - custom mehtod
-(void)submitAuth
{
    if ([self.oCompanyNameTF.text isEqualToString:@""]) {
        [GUATool showDialog:@"请输入公司名称" message:nil waitingTime:1.5];
    }else if ([self.oUanTF.text isEqualToString:@""]) {
        [GUATool showDialog:@"请输入公司统一社会信用代码/注册号" message:nil waitingTime:1.5];
    }else if ([self.oRepresentNameTF.text isEqualToString:@""]) {
        [GUATool showDialog:@"请输入公司法人代表姓名" message:nil waitingTime:1.5];
    }else if (![CommonUtil validateName:self.oRepresentNameTF.text]) {
        [GUATool showDialog:@"姓名应为中文" message:nil waitingTime:1.5];
    }else if ([self.oRepresentPhoneTF.text isEqualToString:@""]) {
        [GUATool showDialog:@"请输入法人代表手机号" message:nil waitingTime:1.5];
    }else if ([self.oRepresentIdNumTF.text isEqualToString:@""]) {
        [GUATool showDialog:@"请输入法人代表身份证号" message:nil waitingTime:1.5];
    }else if (![CommonUtil validateIdentityCard:self.oRepresentIdNumTF.text]) {
        [GUATool showDialog:@"请正确输入法人代表身份证号" message:nil waitingTime:1.5];
    }else if ([self.idImageUrl isEqualToString:@""]) {
        [GUATool showDialog:@"请上传身份证图片" message:nil waitingTime:1.5];
    }else if ([self.licenseImageUrl isEqualToString:@""]) {
        [GUATool showDialog:@"请上传公司营业执照图片" message:nil waitingTime:1.5];
    }else if ([self.permissionImageUrl isEqualToString:@""]) {
        [GUATool showDialog:@"请上传公司开户许可证图片" message:nil waitingTime:1.5];
    }else if (CurentDelegate.account.partyId) {
        if ([self.auditingOpinion isEqualToString:@"0"]) {
            //重新认证
            [KJSRequest OrgReRealAuthWithPartyId:CurentDelegate.account.partyId oCompanyName:self.oCompanyNameTF.text oUan:self.oUanTF.text oRepresentName:self.oRepresentNameTF.text oRepresentPhone:self.oRepresentPhoneTF.text oRepresentIdNum:self.oRepresentIdNumTF.text oRepresentIdNumFile:self.idData oCompanyBusinessLicenseFile:self.licenseData oBankPermitLicenseFile:self.permissionData ret:^(BasicResult *model) {
                NSLog(@"返回的数据=%@",model);
                if (model.success) {
                    
                    [GUATool showDialog:@"实名认证成功" message:nil waitingTime:1.5];
                    if ([CurentDelegate.account.levels intValue] < 1) {
                        CurentDelegate.account.levels = @"1";
                        CurentDelegate.account.type = @"o";
                        CurentDelegate.account.nickName = self.oRepresentNameTF.text;
                        [self dismiss:YES];
                        [self performSelector:@selector(returnFunction) withObject:nil afterDelay:1.5f];
                    }
                }
            }];
        }else{
            [KJSRequest OrgRealAuthWithPartyId:CurentDelegate.account.partyId oCompanyName:self.oCompanyNameTF.text oUan:self.oUanTF.text oRepresentName:self.oRepresentNameTF.text oRepresentPhone:self.oRepresentPhoneTF.text oRepresentIdNum:self.oRepresentIdNumTF.text oRepresentIdNumFile:self.idData oCompanyBusinessLicenseFile:self.licenseData oBankPermitLicenseFile:self.permissionData ret:^(BasicResult *model) {
                NSLog(@"返回的数据=%@",model);
                if (model.success) {
                    
                    [GUATool showDialog:@"实名认证成功" message:nil waitingTime:1.5];
                    if ([CurentDelegate.account.levels intValue] < 1) {
                        CurentDelegate.account.levels = @"1";
                        CurentDelegate.account.type = @"o";
                        CurentDelegate.account.nickName = self.oRepresentNameTF.text;
                        [self dismiss:YES];
                        [self performSelector:@selector(returnFunction) withObject:nil afterDelay:1.5f];
                    }
                }
            }];
        }
        
    }else{
        [GUATool showDialog:@"请登录" message:nil waitingTime:1.5];
    }
}
-(void)returnFunction
{
    if (self.authSuccess) {
        self.authSuccess();
    }
}
- (void)goUploadFile:(id)sender
{
    UITapGestureRecognizer *tap = (UITapGestureRecognizer *)sender;
    
    UIView *myView = (UIView *)tap.view;
    //view的点击效果
    self.identityView.backgroundColor = [[UIColor alloc] initWithRed:255/255.0f green:255/255.0f blue:255/255.0f alpha:1];
    self.licenseView.backgroundColor = [[UIColor alloc] initWithRed:255/255.0f green:255/255.0f blue:255/255.0f alpha:1];
    self.permissionView.backgroundColor = [[UIColor alloc] initWithRed:255/255.0f green:255/255.0f blue:255/255.0f alpha:1];
    myView.backgroundColor = [[UIColor alloc] initWithRed:245/255.0f green:245/255.0f blue:245/255.0f alpha:1];
    switch (myView.tag) {
        case 1:
        {
            NSLog(@"上传身份证");
            UploadIdentityViewController *uploadIdetityVC = [[UIStoryboard storyboardWithName:@"Seconed" bundle:nil] instantiateViewControllerWithIdentifier:@"UploadIdentityViewController"];
            uploadIdetityVC.idImageUrl = self.idImageUrl;
            uploadIdetityVC.uploadeSuccess = ^(NSData *data,NSString *url){
                self.idData = data;
                self.idImageUrl = url;
            };
            [self displayViewController:uploadIdetityVC];
        };
            break;
        case 2:
        {
            NSLog(@"上传执照");
            UploadLicenseViewController *uploadLicenseVC = [[UIStoryboard storyboardWithName:@"Seconed" bundle:nil] instantiateViewControllerWithIdentifier:@"UploadLicenseViewController"];
            uploadLicenseVC.licenseImageUrl = self.licenseImageUrl;
            uploadLicenseVC.uploadeSuccess = ^(NSData *data,NSString *url){
                self.licenseData = data;
                self.licenseImageUrl = url;
                
            };
            [self displayViewController:uploadLicenseVC];
        };
            break;
        case 3:
        {
            NSLog(@"上传许可证");
            UploadPermissionViewController *uploadPermissionVC = [[UIStoryboard storyboardWithName:@"Seconed" bundle:nil] instantiateViewControllerWithIdentifier:@"UploadPermissionViewController"];
            uploadPermissionVC.permissionImageUrl = self.permissionImageUrl;
            uploadPermissionVC.uploadeSuccess = ^(NSData *data,NSString *url){
                self.permissionData = data;
                self.permissionImageUrl = url;
                
            };
            [self displayViewController:uploadPermissionVC];
        };
            break;
        default:
            break;
    }
}

@end
